﻿@using Gurux.DLMS.AMI.Shared.DIs

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Microsoft.AspNetCore.SignalR.Client
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Module;
    using Gurux.DLMS.AMI.Shared.DTOs.Module

@attribute [Authorize(Roles = GXRoles.Admin)]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier
@inject GXModuleService modules
@implements IDisposable
@implements IAmiComponent
@if (Title)
{
    <PageTitle>@Properties.Resources.ModulesTitle</PageTitle>
}
<GXTable @ref="table"
         Context="module"
         ItemsProvider="@GetItems"
         SelectionMode="SelectionMode.Multiple"
         Filter="@Filter"
         Columns="@Columns"
         ShowRemoved="false"
         ShowAllUsers="false"
         CanEdit="@CanEdit"
         OnSearch="@Updated">
    <FilterContent>
        <th>
            <input class="form-control" type="text" placeholder="@Properties.Resources.FilterByName"
                   @onchange="@((ChangeEventArgs __e) => filter.Id = Convert.ToString(__e.Value))" />
        </th>
        <th>
            <input class="form-control" type="text" placeholder="Filter by version..."
                   @onchange="@((ChangeEventArgs __e) => filter.Version = Convert.ToString(__e.Value))" />
        </th>
        <th>
            <input class="form-control" type="text" placeholder="Filter by available version..."
                   @onchange="@((ChangeEventArgs __e) => filter.AvailableVersion = Convert.ToString(__e.Value))" />
        </th>
    </FilterContent>
    <MenuContent>
        <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.AgentGroupManager)>
            <ContextMenuItem Text="@Properties.Resources.Update" Icon="oi oi-pencil" OnClick="@OnUpdate"></ContextMenuItem>
        </AuthorizeView>
    </MenuContent>
    <HeaderContent>
        <th>@Properties.Resources.Name</th>
        <th>@Properties.Resources.Version</th>
        <th>@Properties.Resources.AvailableVersion</th>
    </HeaderContent>
    <ItemContent>
        <td>
            <span class=@(ClientHelpers.GetActiveDot(module.Active))>
            </span><span class="@module.Icon" aria-hidden="true"></span>@module.Id
        </td>
        <td>@module.Version</td>
        <td>@module.AvailableVersion</td>
    </ItemContent>
</GXTable>
<br />
<Confirm @ref="RestartConfirmation"
         ConfirmationChanged="OnRestartConfirmation"
         ConfirmationTitle="Confirm restart"
         OkTitle="@Properties.Resources.Restart"
         ConfirmationMessage="Server needs restart to apply the module changes. Do you want to restart the server now?">
</Confirm>

@code {
    /// <inheritdoc />
    public string Name
    {
        get
        {
            return Gurux.DLMS.AMI.Client.Properties.Resources.UpgradeableModules;
        }
    }

    /// <inheritdoc />
    public Type? ConfigurationUI
    {
        get
        {
            return null;
        }
    }

    /// <inheritdoc />
    public string? Icon
    {
        get
        {
            return "oi oi-puzzle-piece";
        }
    }

    /// <summary>
    /// User is asked should server restart.
    /// </summary>
    protected ConfirmBase? RestartConfirmation;

    /// <summary>
    /// Amount of the modules shown on the view.
    /// </summary>
    [Parameter]
    public int Count { get; set; } = 0;

    /// <summary>
    /// Is filter shown.
    /// </summary>
    [Parameter]
    public bool Filter { get; set; } = true;

    /// <summary>
    /// Is header shown.
    /// </summary>
    [Parameter]
    public bool Header { get; set; } = true;

    /// <summary>
    /// Is edit allowed.
    /// </summary>
    [Parameter]
    public bool CanEdit { get; set; } = true;

    /// <summary>
    /// Available columns.
    /// </summary>
    [Parameter]
    public string[]? Columns { get; set; }

    /// <summary>
    /// Is title shown.
    /// </summary>
    [Parameter]
    public bool Title { get; set; } = true;

    /// <summary>
    /// Module update filter.
    /// </summary>
    private GXModule filter = new GXModule()
        {
            Status = AMI.Shared.DTOs.Enums.ModuleStatus.Installed,
            Active = true,
            NewVersion = true
        };

    /// <summary>
    /// Table reference.
    /// </summary>
    protected GXTable<GXModule>? table;

    /// <summary>
    /// Update table.
    /// </summary>
    protected async Task Updated()
    {
        try
        {
            if (table != null)
            {
                Notifier?.ClearStatus();
                await table.RefreshDataAsync(true);
            }
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    protected override void OnInitialized()
    {
        try
        {
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            Notifier.On<IEnumerable<GXModule>>(this, nameof(IGXHubEvents.ModuleUpdate), async (modules) =>
            {
                await Updated();
            });
            Notifier.Clear();
            Notifier.UpdateButtons();
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    private async ValueTask<ItemsProviderResult<GXModule>> GetItems(GXItemsProviderRequest request)
    {
        //Don't clear status or error is lost.
        Notifier?.ProgressStart();
        try
        {
            ListModules req = new ListModules()
                {
                    Index = request.StartIndex,
                    Count = request.Count,
                    Filter = filter,
                    OrderBy = request.OrderBy,
                    Descending = request.Descending,
                    AllUsers = request.ShowAllUserData
                };
            if (Count != 0)
            {
                req.Count = Count;
            }
            var ret = await Http.PostAsJson<ListModulesResponse>("api/Module/List", req, request.CancellationToken);
            return new ItemsProviderResult<GXModule>(ret.Modules, ret.Count);
        }
        catch (TaskCanceledException)
        {
            //Let the table component handle this.
            throw;
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
        return default;
    }

    void OnClose(object sender)
    {
        ClientHelpers.NavigateTo(NavigationManager, Notifier, "/module");
    }

    /// <summary>
    /// Update selected module.
    /// </summary>
    public async void OnUpdate()
    {
        try
        {
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            if (table?.Active == null)
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            table.Active.Version = table.Active.AvailableVersion;
            UpdateModule req = new UpdateModule() { Module = table.Active };
            var ret = await Http.PostAsJson<UpdateModuleResponse>("api/Module/Install", req);
            if (RestartConfirmation != null && ret.Restart)
            {
                RestartConfirmation.Show();
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Ask should the application restart.
    /// </summary>
    public async Task OnRestartConfirmation(ConfirmArgs args)
    {
        try
        {
            if (args.Confirm)
            {
                StopApplicationRequest req = new StopApplicationRequest();
                await Http.PostAsJson("api/Configuration/Restart", req);
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}
